<?php

/**
 * This is the model class for table "iteam_activity".
 *
 * The followings are the available columns in table 'iteam_activity':
 * @property string $activityid
 * @property string $activityname
 * @property string $serial
 * @property integer $prizeid
 * @property string $class
 * @property string $starttime
 * @property integer $winner
 * @property integer $times
 * @property integer $people
 * @property integer $hongyun
 * @property string $code
 * @property integer $code_time
 * @property string $movie_url
 * @property string $game_url
 * @property string $poll_url
 * @property string $new
 * @property string $notice
 * @property string $endtime
 * @property integer $delaytime
 * @property integer $recommend
 * @property integer $hot
 * @property integer $status
 * @property string $dateline
 * @property string $updated
 */
class Activity extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Activity the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'iteam_activity';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('winner, people, code', 'required'),
            array('prizeid, winner, times, people, hongyun, code_time, delaytime, recommend, hot, status', 'numerical', 'integerOnly' => true),
            array('activityname, movie_url, game_url, poll_url', 'length', 'max' => 200),
            array('serial, code', 'length', 'max' => 30),
            array('class', 'length', 'max' => 20),
            array('starttime, new, notice, endtime, dateline, updated', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('activityid, activityname, serial, prizeid, class, starttime, winner, times, people, hongyun, code, code_time, movie_url, game_url, poll_url, new, notice, endtime, delaytime, recommend, hot, status, dateline, updated', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'prize' => array(self::BELONGS_TO, 'Prize', 'prizeid'),
            'records' => array(self::HAS_MANY, 'Record', 'activityid'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'activityid' => 'ID',
            'activityname' => '活動名稱',
            'serial' => '活動ID',
            'prizeid' => 'Prizeid',
            'class' => '分類',
            'starttime' => '開始時間',
            'winner' => '中獎者數量',
            'times' => '參加次數',
            'people' => '參與人數',
            'hongyun' => '所需鴻運點數',
            'code' => '通關密語',
            'code_time' => '通關密語等待時間',
            'movie_url' => '影片鏈接',
            'game_url' => '遊戲鏈接',
            'poll_url' => '問卷鏈接',
            'new' => '活動資訊',
            'notice' => '活動公告',
            'endtime' => '結束時間',
            'delaytime' => '開獎延時',
            'recommend' => '是否推薦',
            'hot' => '排序',
            'status' => '狀態',
            'dateline' => '創建時間',
            'updated' => '更新時間',            
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('activityid', $this->activityid, true);
        $criteria->compare('activityname', $this->activityname, true);
        $criteria->compare('serial', $this->serial, true);
        $criteria->compare('prizeid', $this->prizeid);
        $criteria->compare('class', $this->class, true);
        $criteria->compare('starttime', $this->starttime, true);
        $criteria->compare('winner', $this->winner);
        $criteria->compare('times', $this->times);
        $criteria->compare('people', $this->people);
        $criteria->compare('hongyun', $this->hongyun);
        $criteria->compare('code', $this->code, true);
        $criteria->compare('code_time', $this->code_time);
        $criteria->compare('movie_url', $this->movie_url, true);
        $criteria->compare('game_url', $this->game_url, true);
        $criteria->compare('poll_url', $this->poll_url, true);
        $criteria->compare('new', $this->new, true);
        $criteria->compare('notice', $this->notice, true);
        $criteria->compare('endtime', $this->endtime, true);
        $criteria->compare('delaytime', $this->delaytime);
        $criteria->compare('recommend', $this->recommend);
        $criteria->compare('hot', $this->hot);
        $criteria->compare('status', $this->status);
        $criteria->compare('dateline', $this->dateline, true);
        $criteria->compare('updated', $this->updated, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = $this->updated = date('Y-m-d H:i:s');
                //生成活動ID
                switch ($this->class){
                    case '人氣限定':
                        $this->serial='P1';
                        break;
                    case '時間限定':
                        $this->serial='T1';
                        break;
                    case '贊助限定':
                        $this->serial='S1';
                        break;
                    default :
                        $this->serial='P1';
                        break;
                }                
                $condition = new CDbCriteria();
                $condition->addCondition("dateline>='".date('Y-m-01')."'");
                $condition->order = 'dateline desc';
                $condition->limit = 1;
                $row = Activity::model()->find($condition);
                unset($condition);
                if($row){
                    $flow = substr($row->serial, 2)+1;
                }else{
                    $flow = date('Ym').'0001';
                }
                $this->serial .= $flow;
            }
            $this->updated = date('Y-m-d H:i:s');
        }
        return true;
    }

}